﻿using pTools.cMethods;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pTools.ChildWin
{
    public partial class YangBan : Form
    {
        string ybCode = "";
        DBHelper db;
        public YangBan(string yCode)
        {
            InitializeComponent();
            ybCode = yCode;
            db = new DBHelper();

        }

        private void YangBan_Load(object sender, EventArgs e)
        {
            try
            {
                loadPrintTemplte();
                loadInfo();
                YangBanProductsInfo();
                loadPrint();
                tbNumber.Text = "1";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// 打印机列表
        /// </summary>
        private void loadPrint()
        {
            PrintDocument print = new PrintDocument();
            string sDefault = print.PrinterSettings.PrinterName;//默认打印机名
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("value");
            foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
            {
                DataRow r = dt.NewRow();
                r["name"] = sPrint;
                r["value"] = sPrint;
                dt.Rows.Add(r);
            }
            cbPrint.DataSource = dt;
            cbPrint.ValueMember = "name";
            cbPrint.DisplayMember = "name";
            cbPrint.SelectedValue = sDefault;
        }
        /// <summary>
        /// 打印模板
        /// </summary>
        private void loadPrintTemplte()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select lbCode,lbName from bar_PrintTemplates_list where status=1");
            // sb.Append(" and lType='" + lType + "'");
            var table = db.getData(sb.ToString(), "");
            cbTemplate.DataSource = table;
            cbTemplate.ValueMember = "lbCode";
            cbTemplate.DisplayMember = "lbName";
        }
        /// <summary>
        /// 根据id获取版辊相关信息
        /// </summary>
        public void loadInfo()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select yCode,yName,weizhi,shuliang,memo from fun_YangBan_info where yCode='" + ybCode + "'");
            var table = db.getData(sb.ToString(), "");
            txt_code.Text = ybCode;
            txt_name.Text = table.Rows[0]["yName"].ToString();
            txt_cf.Text = table.Rows[0]["weizhi"].ToString();
            txt_num.Text = table.Rows[0]["shuliang"].ToString();
            txt_memo.Text = table.Rows[0]["memo"].ToString();
        }
        DataTable dt = new DataTable();
        /// <summary>
        /// 样版存货信息
        /// </summary>
        private void YangBanProductsInfo()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select proName,spec from V_YangBanProducts_info where yCode='" + ybCode + "'");
            dt = db.getData(sb.ToString(), "");
        }

        private void btnPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var table = CommonMethods.YangBanPrintTable();
            #region 填充表格数据
            var r = table.NewRow();
            r["yCode"] = txt_code.Text;
            r["yName"] = txt_name.Text;
            r["weizhi"] = txt_cf.Text;
            r["shuliang"] = txt_num.Text;
            r["memo"] = txt_memo.Text;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                r["name" + i] = dt.Rows[i]["proName"].ToString();
                r["spec" + i] = dt.Rows[i]["spec"].ToString();
            }
            table.Rows.Add(r);
            #endregion
            string info = "";
            var pResult = CommonMethods.DoPrint(table, cbTemplate.Text, cbPrint.Text, ref info);
            if (!pResult)
            {
                MessageBox.Show("条码打印失败：" + info, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
